#include "chatdbhandler.h"
int GetAllUserNameCb(void *data, int col, char **array, char **col_name);


int ChatDbHandler::InsertUserItem(string name, string password){
    string sql_lan = "INSERT INTO USER(NAME, PASSWORD) VALUES ('"
            + name + "','" + password + "');";
    return InsertTableItem(sql_lan.c_str());
}

int ChatDbHandler::GetPwInUserByName(string &a, string item, string condition,  int (*callback)(void*,int,char**, char**)){
    string table = "USER";
    return SelectTableItem(table, item, condition, callback, &a);
}

void ChatDbHandler::CreateUSER(){
    cout << "create" << endl;
    char a[] = "CREATE TABLE IF NOT EXISTS USER "
            "(NAME CHAR[30] PRIMARY KEY,"
            "PASSWORD CHAR[30] NOT NULL);";
    CreateTable(a);
}

int GetAllUserNameCb(void *data, int col, char **array, char **col_name){
    vector<string> *k = (vector<string> *)data;
    string *str = (string *)data;
    k->push_back(array[0]);
    return 0;
}

int ChatDbHandler::GetAllUserName(vector<string> &user_name){
    string table = "USER";
    return SelectTableItem(table, "NAME", "", GetAllUserNameCb, &user_name);
}

